package com.baseweb.webadmin.controller.web;

import com.baseweb.webadmin.controller.BaseController;
import com.baseweb.webadmin.core.model.pojo.Role;
import com.baseweb.webadmin.service.RoleService;
import com.baseweb.webadmin.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by dengsy on 2017/7/9.
 * 系统角色管理
 */
@Controller
@RequestMapping(value = "/systemRole")
public class SystemRoleWebController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(com.baseweb.webadmin.controller.admin.LoginController.class);

    @Autowired
    private RoleService roleService;
    @Autowired
    private UserService userService;

    /**
     * 根据用户id查询特定类型的角色
     */
    @RequestMapping(value = "/findRoleByUserIdType", method = RequestMethod.POST)
    public String findRoleByUserIdType(Model model,@RequestParam("userId") int userId,@RequestParam("roleType") String roleType){
        Role role = roleService.findRoleByUserIdType(userId,roleType);
        model.addAttribute("role",role);
        return "";
    }

    /**
     * 根据type查询角色(分页)
     */
    @RequestMapping(value = "/findAllRoleByTypePage", method = RequestMethod.GET)
    public String findAllRoleByType(Model model, @RequestParam("roleType") String roleType,@RequestParam("page") int page,@RequestParam("pageNum") int pageNum){
        List<Role> list = roleService.findAllRoleByTypePage(roleType,(page-1)*pageNum,pageNum);
        //总记录数
        int numSum = roleService.countRoleByType(roleType);
        //总页数
        int pageSum = (numSum>pageNum?(numSum/pageNum):1);
        if(numSum % pageNum > 0 && numSum>pageNum){
            pageSum += 1;
        }
        model.addAttribute("roleList",list);
        model.addAttribute("numSum",numSum);
        model.addAttribute("pageSum",pageSum);
        return "";
    }

    /**
     * 添加/修改角色
     */
    @RequestMapping(value = "/insertRole", method = RequestMethod.POST)
    public String insertRole(Model model, @ModelAttribute()Role role){
        String isOK = "NO";
        if(role.getId() != null && !"".equals(role.getId())){
            if(roleService.updateRole(role) == 1){
                isOK = "OK";
            }else {
                model.addAttribute("msg","操作失败");
            }
        }else {
            if(roleService.insertRole(role) == 1){
                isOK = "OK";
            }else {
                model.addAttribute("msg","操作失败");
            }
        }
        model.addAttribute("isOK",isOK);
        return "";
    }

    /**
     * 删除角色
     */
    @RequestMapping(value = "/deleteRole", method = RequestMethod.POST)
    public String deleteRole(Model model,@RequestParam("roleId") int roleId){
        int isOK = roleService.deleteRoleById(roleId);
        userService.deleteRoleOpenCityByRoleId(roleId);
        if(isOK == 1){
            model.addAttribute("msg","操作成功");
        }else {
            model.addAttribute("msg","操作失败");
        }
        return "";
    }
}
